home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / xsound.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  3.2 KB  |  167 lines

  1.  
  2. #ifndef _global_h
  3. #    include "global.h"
  4. #endif
  5.  
  6. // to get: FIONREAD
  7. #include <sys/ioctl.h>
  8. // to get: unlink()
  9. // #include <unistd.h>
  10.  
  11. #include <X11/Xlib.h>
  12.  
  13. #ifdef SOLARIS
  14. #    include <sys/filio.h>
  15. #endif
  16.  
  17. #ifdef SCO
  18. #include <sys/socket.h>
  19. #include <sys/select.h>
  20. #endif
  21.  
  22. #define    QUIT    "quit"
  23.  
  24. #define    DBG    0
  25.  
  26. static XKeyboardState    old_keyboard_values;
  27. static Display    *xsound_dpy;
  28. static pid_t pid;
  29. static int pipefd[2];
  30. static int sound_on=0;
  31. static void local_usleep( long time )
  32. {
  33. struct timeval  timeout;
  34. int    nfound;
  35.  
  36.    timeout.tv_sec  = (long)0;
  37.    timeout.tv_usec = (long)time;
  38.  
  39.    nfound=select(0,0,0,0,&timeout);
  40. }
  41.  
  42. static void sound_loop( char *disp_name ) {
  43. XKeyboardControl    values;
  44. char        buffer[80];
  45. int        valid;
  46. int        pitch, percent,duration;
  47. int        pri, old_pri;
  48. int        len;
  49.  
  50.     xsound_dpy = XOpenDisplay(disp_name);
  51.     if (!xsound_dpy) {
  52.         fprintf( stderr, "can't open display\n" );
  53.         exit(-1);
  54.     }
  55.     XGetKeyboardControl(xsound_dpy,&old_keyboard_values);
  56.  
  57.     valid=0;
  58.     do {
  59.         old_pri = -1;
  60.  
  61.         len=read(0,buffer+valid,sizeof(buffer)-valid);
  62.         if (len<=0)        goto exit_sound;
  63. #if (DBG)
  64.         printf( "* at least %d bytes\n", len);
  65.         fflush(stdout);
  66. #endif
  67.  
  68.         do {
  69.             valid += len;
  70.             char    *lf = strchr(buffer,'\n');
  71.  
  72.             while (lf) {
  73.                 *lf = '\0';
  74. #if (DBG)
  75.                 printf( "* %s\n", buffer );
  76.                 fflush(stdout);
  77. #endif
  78.                 if ( !strcmp(buffer,QUIT) )        goto exit_sound;
  79.                 if ( sscanf(buffer,"%d %d %d %d",&pitch,&percent,&duration,&pri)
  80.                                         ==4 ) {
  81.                     if (pri>old_pri) {
  82.                         values.bell_pitch=pitch;
  83.                         values.bell_percent=percent;
  84.                         values.bell_duration=duration;
  85.                         old_pri=pri;
  86.                     }
  87.                 }
  88.                 valid-=(lf-buffer+1);
  89. #if (0)
  90.     //
  91.     // If you got trouble with memmove, than you've got to change this switch
  92.     //
  93.                 memmove(buffer,lf+1,valid);
  94. #else
  95.                 for (int i=0;i<valid;i++)    buffer[i]=lf[i+1];
  96. #endif
  97.  
  98.                 lf = strchr(buffer,'\n');
  99.             }
  100.  
  101.             ioctl(0,FIONREAD,&len);
  102.             if (len)    len=read(0,buffer+valid,sizeof(buffer)-valid);
  103.         } while(len>0);
  104.     
  105. #if (DBG)    
  106.         printf( "* ------------------------\n" );
  107.         fflush(stdout);
  108. #endif
  109.         XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);
  110.         XBell(xsound_dpy,values.bell_percent);
  111.         XFlush(xsound_dpy);
  112.         local_usleep(200000);
  113.     }
  114.     while(1);
  115.  
  116. exit_sound:
  117.  
  118. #if (0)
  119.     values.bell_pitch    = old_keyboard_values.bell_pitch;
  120.     values.bell_percent  = old_keyboard_values.bell_percent;
  121.     values.bell_duration = old_keyboard_values.bell_duration;
  122. #else
  123.     values.bell_pitch    = 400;
  124.     values.bell_percent  =  50;
  125.     values.bell_duration = 100;
  126. #endif
  127.     XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);
  128.  
  129.     XCloseDisplay( xsound_dpy );
  130.     printf( "* subprocess xsound termiated\n" );
  131.     exit(0);
  132. }
  133.  
  134.  
  135.  
  136. void init_sound( char *disp_name ) {
  137.  
  138.     pipe (pipefd);
  139.  
  140.     pid=fork();
  141.     if (pid == (pid_t)0) {
  142.         close(0);                    // stdin schlieâ–€en        (Sohn)
  143.         dup (pipefd[0]);
  144.         close (pipefd[1]);
  145.         sound_loop(disp_name);
  146.     }
  147.     else {
  148.         close (pipefd[0]);        //                                (Vater)
  149.         sound_on = 1;
  150.     }
  151. }
  152.  
  153. void end_sound() {
  154.     if (!sound_on)        return;
  155.     write( pipefd[1], QUIT "\n", strlen(QUIT)+1 );
  156.     close( pipefd[1] );
  157. }
  158.  
  159.  
  160. void do_sound( int pitch, int percent, int duration, int pri ) {
  161. char buffer[80];
  162.  
  163.     if (!sound_on)        return;
  164.     sprintf( buffer, "%d %d %d %d\n", pitch, percent, duration, pri );
  165.     write( pipefd[1],buffer,strlen(buffer));
  166. }
  167.